package cn.codexin.wechatofficial.controller;

import cn.codexin.wechatofficial.model.dbo.*;
import cn.codexin.wechatofficial.service.HistoricalService;
import cn.codexin.wechatofficial.service.TimeService;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * @author ly
 * @date 2020/8/10 0010 13:48
 * 有关历史数据的接口
 */
@Slf4j
@RestController
@RequestMapping(value = "history", produces = "application/json;charset=UTF-8")
public class HistoricalDataController {
    private final static Logger logger = LoggerFactory.getLogger(HistoricalDataController.class);

    @Autowired
    private HistoricalService historicalService;
    @Autowired
    private TimeService timeService;


    /*查询当前房间的所有遥测监测点*/
    @GetMapping("allMVDots")
    public Map<String,Object> allMVDots(@RequestParam int ModuleID){
        Map<String,Object> map = new HashMap<>();
        try {
            List<MVDefPojo> mvDefPojoList = historicalService.allMVDot(ModuleID);
            if (mvDefPojoList.size()==0){
                throw new RuntimeException("该房间还未设置遥测监测点,请联系管理员！");
            }else {
                map.put("state",true);
                map.put("msg","查询成功");
                map.put("mvDefPojoList",mvDefPojoList);
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","查询失败,提示："+e.getMessage());
        }
        return map;
    }
    /*查询当前房间的所有遥信监测点*/
    @GetMapping("allSPSDots")
    public Map<String,Object> allSPSDots(@RequestParam int ModuleID){
        Map<String,Object> map = new HashMap<>();
        try {
            List<SPSDefPojo> spsDefPojoList = historicalService.allSPSDots(ModuleID);
            if (spsDefPojoList.size()==0){
                throw new RuntimeException("该房间还未设置遥信监测点,请联系管理员！");
            }else {
                map.put("state",true);
                map.put("msg","查询成功");
                map.put("spsDefPojoList",spsDefPojoList);
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","查询失败,提示："+e.getMessage());
        }
        return map;
    }
    /*查询当前房间的所有告警点*/
    @GetMapping("allAlarmDots")
    public Map<String,Object> allAlarmDots(@RequestParam int ModuleID){
        Map<String,Object> map = new HashMap<>();
        try {
            List<AlarmDefPojo> alarmDefPojoList = historicalService.allAlarmDots(ModuleID);
            if (alarmDefPojoList.size()==0){
                throw new RuntimeException("该房间还未设置告警点,请联系管理员！");
            }else {
                map.put("state",true);
                map.put("msg","查询成功");
                map.put("spsDefPojoList",alarmDefPojoList);
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","查询失败,提示："+e.getMessage());
        }
        return map;
    }


    /*遥测曲线和遥测报表*/
    @GetMapping("telemetry")
    public Map<String,Object> telemetryCurve(@RequestParam int DotID,long startTime,long endTime){
        Map<String,Object> map = new HashMap<>();
        try {
            List<MVValuePojo> mvValuePojoList = historicalService.telemetry(DotID,startTime,endTime);
            if (mvValuePojoList.size()==0){
                throw new RuntimeException("该遥测监测点这段时间内未上传数据，请联系管理员核实！");
            }else {
                map.put("state",true);
                map.put("msg","查询成功");
                map.put("mvValuePojoList",mvValuePojoList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","查询失败,提示："+e.getMessage());
        }
        return map;
    }
    /*遥测报表*/

    @GetMapping("telemetryForm")
    public Map<String,Object> telemetryForm(HttpServletRequest request){
        String openid = request.getHeader("openid");
        System.out.println(openid);
        logger.info("ffffffffff");
        log.info("毫秒数");
        /*String openid = (String)request.getSession().getAttribute("openid");
        System.out.println(openid);*/
        System.out.println(System.currentTimeMillis());
        Map<String,Object> map = new HashMap<>();
        map.put("毫秒数",System.currentTimeMillis());
        return map;
    }



    /*变位记录*/
    @GetMapping("/variantRecord")
    public Map<String,Object> variantRecord(@RequestParam int DotID,int ModuleID,long startTime,long endTime){
        Map<String,Object> map = new HashMap<>();
        try {
            List<SPSValuePojo> spsValuePojoList = historicalService.variantRecord(DotID,ModuleID,startTime,endTime);
            if (spsValuePojoList.size()==0){
                throw new RuntimeException("该遥信监测点这段时间内未上传数据，请联系管理员核实！");
            }else {
                map.put("state",true);
                map.put("msg","查询成功");
                map.put("spsValuePojoList",spsValuePojoList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","查询失败,提示："+e.getMessage());
        }
        return map;
    }
    /*告警记录*/
    @GetMapping("/warningRecord")
    public Map<String,Object> warningRecord(@RequestParam int DotID,int ModuleID,int DotType,long startTime,long endTime){
        Map<String,Object> map = new HashMap<>();
        try {
            List<AlarmValuePojo> alarmValuePojoList = historicalService.warningRecord(DotID,ModuleID,DotType,startTime,endTime);
            if (alarmValuePojoList.size()==0){
                throw new RuntimeException("该告警点无报警信息");
            }else {
                map.put("state",true);
                map.put("msg","查询成功");
                map.put("alarmValuePojoList",alarmValuePojoList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","查询失败,提示："+e.getMessage());
        }
        return map;
    }


}
